﻿/*
    Copyright (c) 2009 Chris Umbel

    Permission is hereby granted, free of charge, to any person obtaining a 
	copy of this software and associated documentation files (the "Software"), 
	to deal in the Software without restriction, including without limitation 
	the rights to use, copy, modify, merge, publish, distribute, sublicense, 
	and/or sell copies of the Software, and to permit persons to whom the Software 
	is furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in all 
	copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 
	FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 
	COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
	WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 
	IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.ReportingServices.DataProcessing;
using System.Net;
using System.IO;

namespace CouchDBExtension
{
    public class CouchDBCommand : IDbCommand
    {
        private CouchDBParameterCollection _parameters;
        private CouchDBConnection _connection;

        public Microsoft.ReportingServices.DataProcessing.IDbTransaction Transaction { get; set; }
        public Microsoft.ReportingServices.DataProcessing.CommandType CommandType { get; set; }
        public int CommandTimeout { get; set; }
        public string CommandText { get; set; }

        public IDataParameterCollection Parameters
        {
            get
            {
                return _parameters;
            }
        }

        public CouchDBCommand(CouchDBConnection connection)
        {
            _parameters = new CouchDBParameterCollection();
            _connection = connection;
        }

        public IDataParameter CreateParameter()
        {
            return new CouchDBParameter();
        }

        /* run the command */
        public IDataReader ExecuteReader(CommandBehavior behavior)
        {
            string command = this.CommandText;

            /* if the report designer is just looking for the schema we'll dump parameters */
            if (behavior == CommandBehavior.SchemaOnly)
            {
                command = this.CommandText;

                int queryStringPosition = this.CommandText.IndexOf("?");

                if(queryStringPosition != -1)
                    command = command.Substring(0, queryStringPosition); 
            }
            else 
            { 
                /* replace parameters with actual values */
                foreach (CouchDBParameter parameter in this.Parameters)
                {
                    command = this.CommandText.Replace(parameter.ParameterName, parameter.Value.ToString());
                }
            }

            /* RESTfully execute our command on the couchdb server */
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(
                new Uri(
                    new Uri(_connection.ConnectionString), 
                    command)
                );
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            /* read the response */
            return new CouchDBReader(response.GetResponseStream());
        }

        public void Cancel() { }

        public void Dispose() { }
    }
}
